package com.qyhl.cloud.webtv.module_integral.wallet.exchangegold;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.cloud.webtv.module_integral.R;
import com.qyhl.cloud.webtv.module_integral.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.intergral.IntegralUserInfoBean;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;

/**
 * 兑换零钱
 */
@Route( path = ARouterPathConstant.EXCHANGE_GOLD)
public class ExchangeGold extends BaseActivity implements ExchangeGoldContract.ExchangeGoldView {
    private ExchangeGoldContract.ExchangeGoldPresenter presenter;
    @BindView(R2.id.exchangehave)
    TextView exchangehave;
    @BindView(R2.id.exchangeabout)
    TextView exchangeabout;
    @BindView(R2.id.exchangecoinRate)
    TextView exchangecoinRate;
    @BindView(R2.id.exchangeedit)
    EditText exchangeedit;

    //当前总金币
    private int nowCoin = 0;
    //汇率
    private int coinRate = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }



    private void initView() {
        presenter = new ExchangeGoldPresenter(this);
        presenter.getIntegralData();

        exchangeedit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = exchangeedit.getText().toString();
                if (TextUtils.isEmpty(text)) {
                    exchangeabout.setText("");
                } else {
                    int num = Integer.parseInt(text);
                    if (num > nowCoin) {
                        num = nowCoin;
                    }
                    if (!text.equals(String.valueOf(num))) {
                        exchangeedit.setText(String.valueOf(num));
                        //将光标移至文字末尾
                        exchangeedit.setSelection(String.valueOf(num).length());
                    }
                    DecimalFormat df = new DecimalFormat("#.00##");
                    if (coinRate > 0) {
                        //coinRate>0表示请求结果回来了
                        exchangeabout.setText("≈" + df.format(num / (float) coinRate) + "元");
                    }
                }
            }
        });
    }

    @OnClick({R2.id.back, R2.id.exchangeall, R2.id.commit})
    public void onclick(View view) {
        int i = view.getId();
        if (i == R.id.back) {
            finish();

        } else if (i == R.id.exchangeall) {
            exchangeedit.setText(String.valueOf(nowCoin));

        } else if (i == R.id.commit) {
            if (coinRate > 0) {
                //coinRate>0表示请求结果回来了
                String text = exchangeedit.getText().toString();
                if (TextUtils.isEmpty(text)) {
                    Toasty.normal(this, "请输入兑换金币数").show();
                } else {
                    int num = Integer.parseInt(text);
                    int ceil = (int) Math.ceil(coinRate / (double) 10);
                    if ((num < ceil)) {
                        Toasty.normal(this, "兑换金币数量不足" + ceil).show();
                    } else {
                        presenter.exchange(text);
                    }
                }
            }

        } else {
        }
    }


    /**
     * 积分数据请求失败
     */
    @Override
    public void integralError(String msg) {
        Toasty.normal(this, msg).show();
    }

    /**
     * 积分数据请求成功
     */
    @Override
    public void integralSuccess(IntegralUserInfoBean data) {
        nowCoin = data.getCurrCoin() == null ? 0 : data.getCurrCoin();
        exchangehave.setText("可兑换金币数量：" + nowCoin + "个");
        coinRate = data.getCoinRate();
        exchangecoinRate.setText("金币比值：1:" + coinRate);
    }

    @Override
    public void exchangeError(String message) {
        Toasty.normal(this, message).show();
    }

    @Override
    public void exchangeSuccess() {
        ActionLogUtils.getInstance().onEvent(ActionConstant.EXCHANGE_GOLD);
        Toasty.normal(this, "兑换成功").show();
        setResult(666);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }

    @Override
    protected int getContentView() {
        return R.layout.inter_activity_exchange_gold;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        initView();
    }

    @Override
    protected void setListener() {

    }
}
